﻿using CoreModule;
using HandyControl.Controls;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace UIModule.Views
{
    /// <summary>
    /// ucHistory.xaml 的交互逻辑
    /// </summary>
    public partial class History : UserControl
    {
        public History()
        {
            InitializeComponent();
        }
         
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DefaultPaginationButtonStyle = this.FindResource("DefaultPaginationButtonStyle") as Style;

            UpdatePaginationStyle(this.pgtList);
        }

        private void ListViewMeasurementInfos_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void BtnSecondary_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var btn = sender as System.Windows.Controls.Button;
            btn.Background = new SolidColorBrush(Color.FromRgb(0xF1, 0xF0, 0xF5));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSecondary_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var btn = sender as System.Windows.Controls.Button;
            btn.Background = new SolidColorBrush(Colors.Transparent);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
            UpdatePaginationStyle(this.pgtList);
        }

        private void datePicker_Start_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var start = datePicker_Start.SelectedDate;

            if (start.HasValue)
            {
                this.datePicker_End.BlackoutDates.Clear();
                this.datePicker_End.BlackoutDates.Add(new CalendarDateRange(DateTime.MinValue, start.Value.AddDays(-1)));
            }
            else
            {
                this.datePicker_End.BlackoutDates.Clear();
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datePicker_End_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            var end = datePicker_End.SelectedDate;

            if (end.HasValue)
            {

                this.datePicker_Start.BlackoutDates.Add(new CalendarDateRange(end.Value.AddDays(1), DateTime.MaxValue));
            }
            else
            {
                this.datePicker_Start.BlackoutDates.Clear();
            }
        }
        Style DefaultPaginationButtonStyle { get; set; } = default(Style);
        private void UpdatePaginationStyle(Pagination pagination)
        {
            var panelMain = (StackPanel)pagination.Template.FindName("PART_PanelMain", pagination);

            foreach (RadioButton item in panelMain.Children)
            {
                item.Style = DefaultPaginationButtonStyle;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pgtList_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
        {
            UpdatePaginationStyle(sender as Pagination);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="navigationContext"></param>
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var list = navigationContext.Parameters[CoreConstant.MeasurementListKey] as List<CoreModule.BloodGlucoseMeasurementInfo>;

            if (list == null || list.Count==0)
            {

                PaginationArea.Visibility = Visibility.Hidden;
                this.gridEmpty.Visibility = Visibility.Visible;
                return;
            }
            else
            {
                PaginationArea.Visibility = Visibility.Visible;
                this.gridEmpty.Visibility = Visibility.Hidden;
                return;
            }

        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnReset_Click(object sender, RoutedEventArgs e)
        {
            UpdatePaginationStyle(this.pgtList);
        }
    }
}
